﻿#include "stageview.h"
#include <QPainter>

const int X_RANGE = 120;
const int Y_RANGE = 120;

const double DEFAULT_NOZZLE_SPACING = 8.0; // 喷嘴间隔 8 mm
const double DEFAULT_NOZZLE_ORIG_X = 17.95; // 0,0 喷嘴的位置
const double DEFAULT_NOZZLE_ORIG_Y = 41.60;

const int DEFAULT_COL_COUNT = 8;
const int DEFAULT_ROW_COUNT = 4;

StageView::StageView(QWidget *parent)
    : QWidget(parent),
      m_rowCount(DEFAULT_ROW_COUNT),
      m_colCount(DEFAULT_COL_COUNT),
      m_nozzleZeroPosX(DEFAULT_NOZZLE_ORIG_X),
      m_nozzleZeroPosY(DEFAULT_NOZZLE_ORIG_Y),
      m_colSpacing(DEFAULT_NOZZLE_SPACING),
      m_rowSpacing(DEFAULT_NOZZLE_SPACING),
      m_defaultX(DEFAULT_NOZZLE_ORIG_X),
      m_defaultY(DEFAULT_NOZZLE_ORIG_Y),
      m_currentX(0),
      m_currentY(0)
{
    setFixedSize(X_RANGE * 2, Y_RANGE * 2);
}

StageView::~StageView()
{

}

void StageView::setNozzleZeroPos()
{
    m_nozzleZeroPosX = m_currentX;
    m_nozzleZeroPosY = m_currentY;
    update();
}

void StageView::setNozzleZeroPos(double x_mm, double y_mm)
{
    m_nozzleZeroPosX = x_mm;
    m_nozzleZeroPosY = y_mm;
    update();
}

void StageView::loadSettings(QSettings &settings)
{
    m_colCount = settings.value("NozzleHolder/colCount", DEFAULT_COL_COUNT).toInt();
    m_rowCount = settings.value("NozzleHolder/rowCount", DEFAULT_ROW_COUNT).toInt();

    m_colSpacing = settings.value("NozzleHolder/colSpacing", DEFAULT_NOZZLE_SPACING).toDouble();
    m_rowSpacing = settings.value("NozzleHolder/rowSpacing", DEFAULT_NOZZLE_SPACING).toDouble();

    m_nozzleZeroPosX = settings.value("NozzleHolder/originX", DEFAULT_NOZZLE_ORIG_X).toDouble();
    m_nozzleZeroPosY = settings.value("NozzleHolder/originY", DEFAULT_NOZZLE_ORIG_Y).toDouble();

    m_defaultX = settings.value("NozzleHolder/defaultX", DEFAULT_NOZZLE_ORIG_X).toDouble();
    m_defaultY = settings.value("NozzleHolder/defaultY", DEFAULT_NOZZLE_ORIG_Y).toDouble();

}

void StageView::resetNozzleZeroPos()
{
    m_nozzleZeroPosX = m_defaultX;
    m_nozzleZeroPosY = m_defaultY;
    update();
}

void StageView::setRowCol(int rows, int cols)
{
    m_rowCount = rows;
    m_colCount = cols;
}

void StageView::setPosition(double x_mm, double y_mm)
{
    m_currentX = x_mm;
    m_currentY = y_mm;
    update();
}

void StageView::drawCross(QPainter &painter, const QPointF &center, int radius)
{
    int x = center.x();
    int x1 = x - radius;
    int x2 = x + radius;

    int y = center.y();
    int y1 = y - radius;
    int y2 = y + radius;

    painter.drawLine(QPoint(x1, y), QPoint(x2, y));
    painter.drawLine(QPoint(x, y1), QPoint(x, y2));
}

void StageView::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.fillRect(0, 0, width(), height(), Qt::white);

    //painter.setWindow(QRect(0, 0, X_RANGE, Y_RANGE));

    for(int col = 0; col < m_colCount; col++)
    {
        for(int row = 0; row < m_rowCount; row++)
        {
            double x = 2 * (m_nozzleZeroPosX + col * m_colSpacing);
            double y = 2 * (m_nozzleZeroPosY + row * m_rowSpacing);
            painter.drawEllipse(QPointF(x, y) , 5, 5);
            drawCross(painter, QPointF(x, y), 7);
        }
    }
    painter.setPen(Qt::red);
    drawCross(painter, QPointF(2 * m_currentX, 2 * m_currentY), 3);
}
